package com.dx.mes.service.query.service;

import static com.dx.mes.common.bean.def.GenericDef.E_FIPINQBOX_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;

import java.util.ArrayList;
import java.util.List;

import com.dx.common.table.ppt.Bis_ope;
import com.dx.common.table.ppt.Bis_opeId;
import com.dx.mes.common.dao.IBisOpeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.his.His_ret_prd_info;
import com.dx.mes.common.dao.IHisRetPrdInfoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IHipshthisService;
import com.dx.mes.facade.query.tx.Hipshthis.HipshthisI;
import com.dx.mes.facade.query.tx.Hipshthis.HipshthisO;
import com.dx.mes.facade.query.tx.Hipshthis.HipshthisOA;

@Service("hipshthisService")
public class HipshthisService implements IHipshthisService{
	private LogUtils logUtils;
	private static final String TX_NAME = "HPSHTHIS";

//	@Resource
//	private SessionFactory sessionFactoryHis;
	@Autowired
	private IHisRetPrdInfoDao hisRetPrdInfoDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Override
	public String subMainProc(String evt_no, String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FipinqboxService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FipinqboxService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
//		Session session = sessionFactoryHis.getCurrentSession();
		String outMsg = "";
		
		HipshthisI inTrx = new HipshthisI();
		HipshthisO outTrx = new HipshthisO();

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		try {
			inTrx = JacksonUtil.fromJson(strInMsg, HipshthisI.class);

			subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
//				session.flush();
				hisRetPrdInfoDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private void subMainProc2(char actionType, HipshthisI inTrx, HipshthisO outTrx) {
	
		switch (actionType) {
		case 'T': {
			getHisPrdTabByCondition(inTrx,outTrx);
			break;
		}
		default: {
			String gszMsgBuf = "Invalide type input: [" + actionType + "]";
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT));
		}
		}
	}

	private void getHisPrdTabByCondition(HipshthisI inTrx, HipshthisO outTrx) {
//		Session session = sessionFactoryHis.openSession();
		String sql = "FROM His_ret_prd_info WHERE 1=1 ";
		if (!Util.isSpaceCheck(inTrx.getPrd_seq_id())) {
			sql += "AND prd_seq_id = '"+inTrx.getPrd_seq_id()+"' order by evt_timestamp desc";
		}
//		Query query = session.createQuery(sql);
//		List<His_ret_prd_info> his_ret_prd_infos = query.list();
		List<His_ret_prd_info> his_ret_prd_infos = hisRetPrdInfoDao.find(sql);
		if (his_ret_prd_infos.isEmpty()) {
			outTrx.setHisCnt(0);
		} else {
			outTrx.setHisCnt(his_ret_prd_infos.size());
			List<HipshthisOA> oary_his = new ArrayList<HipshthisOA>();
//			outTrx.setOary_his(his_ret_prd_infos);
			for(His_ret_prd_info his_ret_prd_info : his_ret_prd_infos){
				HipshthisOA oary = new HipshthisOA();
				oary.setEvt_timestamp(his_ret_prd_info.getId().getEvt_timestamp().toString());
				oary.setPrd_seq_id(his_ret_prd_info.getPrd_seq_id());
				oary.setBox_id_fk(his_ret_prd_info.getBox_id_fk());
				oary.setOpe_evt_name(his_ret_prd_info.getOpe_evt_name());
				oary.setOpe_evt_node(his_ret_prd_info.getOpe_evt_no());
				oary.setOpe_evt_usr(his_ret_prd_info.getOpe_evt_usr());
				oary.setOperation(his_ret_prd_info.getOperation());
				oary.setEvt_cate(his_ret_prd_info.getEvt_cate());
				oary.setEvt_usr(his_ret_prd_info.getEvt_usr());
				oary.setCr_ope_id_fk(his_ret_prd_info.getCr_ope_id_fk());
				oary.setCr_ope_no_fk(his_ret_prd_info.getCr_ope_no_fk());
				oary.setCr_ope_ver_fk(his_ret_prd_info.getCr_ope_ver_fk());
				Bis_opeId  bis_cr_opeId = new Bis_opeId();
				bis_cr_opeId.setOpe_id(his_ret_prd_info.getCr_ope_id_fk());
				bis_cr_opeId.setOpe_ver(his_ret_prd_info.getCr_ope_ver_fk());
				Bis_ope bis_cr_ope = bisOpeDao.get(bis_cr_opeId);
				if(bis_cr_ope!=null){
					oary.setCr_ope_dsc(bis_cr_ope.getOpe_dsc());
				}
				oary.setCr_path_id_fk(his_ret_prd_info.getCr_path_id_fk());
				oary.setCr_path_ver_fk(his_ret_prd_info.getCr_path_ver_fk());
				oary.setCr_proc_id_fk(his_ret_prd_info.getCr_proc_id_fk());
				oary.setTool_id_fk(his_ret_prd_info.getCr_tool_id_fk());
				oary.setCr_tool_run_mode(his_ret_prd_info.getCr_tool_run_mode());
				oary.setNx_ope_id_fk(his_ret_prd_info.getNx_ope_id_fk());
				oary.setNx_ope_no_fk(his_ret_prd_info.getNx_ope_no_fk());
				oary.setNx_ope_ver_fk(his_ret_prd_info.getNx_ope_ver_fk());
				Bis_opeId  bis_nx_opeId = new Bis_opeId();
				bis_nx_opeId.setOpe_id(his_ret_prd_info.getNx_ope_id_fk());
				bis_nx_opeId.setOpe_ver(his_ret_prd_info.getNx_ope_ver_fk());
				Bis_ope bis_nx_ope = bisOpeDao.get(bis_nx_opeId);
				if(bis_nx_ope!=null){
					oary.setNx_ope_dsc(bis_nx_ope.getOpe_dsc());
				}
				oary.setNx_path_id_fk(his_ret_prd_info.getNx_path_id_fk());
				oary.setNx_path_ver_fk(his_ret_prd_info.getNx_path_ver_fk());
				oary.setNx_proc_id_fk(his_ret_prd_info.getNx_proc_id_fk());
				oary.setNx_tool_id_fk(his_ret_prd_info.getNx_tool_id_fk());
				oary.setNx_tool_run_mode(his_ret_prd_info.getNx_tool_run_mode());
				oary.setMdl_id_fk(his_ret_prd_info.getMdl_id_fk());
				oary.setWo_id_fk(his_ret_prd_info.getWo_id_fk());
				oary.setPrd_stat(his_ret_prd_info.getPrd_stat());
				oary.setPrd_grade(his_ret_prd_info.getPrd_grade());
				oary.setPrd_qty(his_ret_prd_info.getPrd_qty());
				oary.setPrd_thickness(his_ret_prd_info.getPrd_thickness());
				oary.setSlot_no(his_ret_prd_info.getSlot_no());
				oary.setTool_port_id_fk(his_ret_prd_info.getTool_port_id_fk());
				oary.setShip_box_id(his_ret_prd_info.getShip_box_id_fk());
				oary_his.add(oary);
			}
			outTrx.setOary_his(oary_his);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

}
